<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: TUser.php
// +----------------------------------------------------------------------
// | Author: liuju
// +----------------------------------------------------------------------
// | Description: 描述文档功能
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
// +----------------------------------------------------------------------
// | 1.Date: 2019-02-28 Author: liuju Modification: 创建
// +----------------------------------------------------------------------

namespace App\ModelTransfers\Traits;


use App\Models\User;
use App\Models\UserGlobal;
use Illuminate\Support\Facades\DB;

trait TUser
{
    /**
     * Description:
     * Author: liuju
     * DateTime: 2019-02-28 10:40
     * @param  $tUser
     * @param  $userPhoneList
     * @return int|mixed
     * @throws \Throwable
     */
    public static function transfer($tUser, $userPhoneList)
    {
        $avatar = $tUser->avatar;
        $userId = '';
        if (in_array($tUser->phone, $userPhoneList)) {
            $user = User::where('phone_num', $tUser->phone)->first();
            if (!$user->avatar) {
                $user->avatar =$avatar ? $avatar->oss_url : '';
            }
            $user->old_kskd_user_id = $tUser->id;
            $user->saveOrFail();
            $userId = $user->id;
        } else {
           $newId =  DB::table('users')->insertGetId([
                'phone_num' => $tUser->phone,
                'password' => $tUser->password,
                'avatar' => $avatar ? $avatar->oss_url : '',
                'source' => $tUser->register_channel,
                'created_at' => $tUser->create_date,
                'updated_at' => $tUser->update_date,
                'old_kskd_user_id' => $tUser->id,
            ]);
           $userId = $newId;
        }
        return $userId;
    }

    public static function getGlobalStatus($status)
    {
        $newStatus = 0;
//        0： 初始化
//1：提单
//2：拒绝
//3：通过
//4：超时
//5：已要款
//6：待放款
//7：已放款（还款中）
//8：还款完成
//9：逾期
//10：放款失败
        switch (intval($status)) {

            case    1:
                $newStatus = UserGlobal::STATUS_LOAN_REQUEST;
                break;
            case   2:
                $newStatus = UserGlobal::STATUS_LOAN_REQUEST_FAILED;
                break;
            case   3:
                $newStatus = UserGlobal::STATUS_LOAN_REQUEST_SUCCESS;
                break;
            case   4:
                $newStatus = UserGlobal::STATUS_LOAN_RESTART;
                break;
            case   5:
            case   6:
                $newStatus = UserGlobal::STATUS_LOAN_MONEY_PROCESSING;
                break;
            case 10:
                $newStatus = UserGlobal::STATUS_LOAN_MONEY_FAIL;
                break;
        }
        return $newStatus;
    }

//    /**
//     * 默认初始化首页
//     */
//    const STATUS_NOTHING = 0;
//
//    /**
//     * 借款已提交审核
//     */
//    const STATUS_LOAN_REQUEST = 1;
//
//    /**
//     * 借款审核未通过
//     */
//    const STATUS_LOAN_REQUEST_FAILED = 2;
//
//    /**
//     * 借款审核通过
//     */
//    const STATUS_LOAN_REQUEST_SUCCESS = 3;
//
//    /**
//     * 收款超时
//     */
//    const STATUS_LOAN_TIME_OUT = 4;
//
//    /**
//     * 收款完全超时
//     */
//    const STATUS_LOAN_RESTART = 5;
//
//    /**
//     * 正在放款中
//     */
//    const STATUS_LOAN_MONEY_PROCESSING = 40;
//
//
//    /**
//     * 放款失败
//     */
//    const STATUS_LOAN_MONEY_FAIL = 45;
//
//    /**
//     * 放款失败，需要换卡
//     */
//    const STATUS_LOAN_CHANGE_CARD =50;
//
//    /**
//     * 还款初始
//     */
//    const STATUS_REPAY_INIT = 61;
//
//    /**
//     * 还款中
//     */
//    const STATUS_REPAY_ING = 65;
//
//    /**
//     * 还款成功
//     */
//    const STATUS_REPAY_SUCCESS = 70;
//
//    /**
//     * 还款失败
//     */
//    const STATUS_REPAY_FAILURE = 75;
//
//    /**
//     * 全部结清
//     */
//    const STATUS_REPAY_OVER = 80;

}